package com.lins.common.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.lins.common.common.enums.ExceptionCode;
import com.lins.common.common.exception.BusinessException;

import java.util.Map;

/**
 * @author linsir
 * @title: CheckUtils
 * @projectName lins
 * @description: TODO
 * @date 2021/12/9 9:35
 */
public class CheckUtils {

        /**
         * 参数非空校验
         *
         * @param object
         * @param attributes
         */
        public static void checkNotNull(Object object, String... attributes) {

                if (ObjectUtil.isNull(object)) {
                        throw new BusinessException(ExceptionCode.PARAM_IS_NULL);
                }
                if (attributes != null && attributes.length > 0) {
                        for (String attribute : attributes) {
                                Object fieldValue;
                                if (object instanceof Map) {
                                        fieldValue = ((Map) object).get(attribute);
                                } else {
                                        fieldValue = ReflectUtil.getFieldValue(object, attribute);
                                }
                                if (ObjectUtil.isEmpty(fieldValue)) {
                                        throw new BusinessException(ExceptionCode.PARAM_IS_NULL.fillMessage(attribute));
                                }

                        }
                }
        }

}
